package io.github.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.github.dao.SysGatewayDao;
import io.github.entity.SysGatewayEntity;
import io.github.service.SysGatewayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class SysGatewayServiceImpl extends ServiceImpl<SysGatewayDao, SysGatewayEntity> implements SysGatewayService {
    @Resource
    private SysGatewayDao dao;

    @Override
    public List<SysGatewayEntity> queryListByPage(String name) {
        Wrapper<SysGatewayEntity> wrapper = new EntityWrapper<>();

        if(StringUtils.isNotBlank(name)){
            wrapper.like("name",name);
        }
        return this.selectList(wrapper);
    }

    @Override
    public void save(SysGatewayEntity gatewayEntity) {
        baseMapper.insert(gatewayEntity);
    }

    @Override
    public void deleteBatch(String[] ids) {
        baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public void update(SysGatewayEntity entity, Integer id) {
        dao.updateGateway(entity,id);
    }

    @Override
    public SysGatewayEntity getById(Integer id) {
        return dao.selectById(id);
    }

    @Override
    public String getByRemark(String remark){
        Wrapper<SysGatewayEntity> wrapper = new EntityWrapper();
        wrapper.eq("remark",remark);
        SysGatewayEntity entity =  baseMapper.selectList(wrapper).get(0);
        Integer id = entity.getId();
        String name = entity.getName();
        return id+","+name;
    }

    @Override
    public String getByName(String name){
        Wrapper<SysGatewayEntity> wrapper = new EntityWrapper();
        wrapper.eq("name",name);
        SysGatewayEntity entity = baseMapper.selectList(wrapper).get(0);
        Integer id = entity.getId();
        String remark = entity.getRemark();
        return id+","+remark;
    }

}
